package com.cp.screen;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

public class ScreenResolutionDipActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TextView textView = (TextView) this.findViewById(R.id.textview);
		int height = getWindowManager().getDefaultDisplay().getHeight();
		int width = getWindowManager().getDefaultDisplay().getWidth();

		int heightDip = px2dip(this, height);
		int widthDip = px2dip(this, width);
		textView.setText("height = " + height + "px" + "\r\n" + "width = "
				+ width + "px" + "\r\n" + "heightDip = " + heightDip + "dip"
				+ "\r\n" + "widthDip = " + widthDip + "dip" + "\r\n");
	}

	public static int px2dip(Context context, float pxValue) {
		if (null == context || pxValue < 0)
			return 0;
		final float scale = context.getResources().getDisplayMetrics().density;

		return (int) ((pxValue - 0.5f) / scale);
	}
}